﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using EasyStep.Business;

using EasyStep.Website.DependencyResolvers;
using EasyStep.Business.Interfaces;
using EasyStep.Business.Managers;
using EasyStep.Website.Controllers;
using System.Configuration;
using EasyStep.Data.Entities;
using EasyStep.Website.Models;
using EasyStep.Business.Constants;
using EasyStep.Common;

namespace EasyStep.Website
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

        protected void Application_Start()
        {
            TypeFinder.Initialize(System.Configuration.ConfigurationManager.AppSettings["TestActionsAssembliesPath"]);
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            ConfigureBundleTables();

            // Main application service initialization
            Dictionary<string, string> configuration = new Dictionary<string, string>();
            configuration.Add(MainApplicationServiceConstants.FileSystemDataStoragePath, System.Configuration.ConfigurationManager.AppSettings[MainApplicationServiceConstants.FileSystemDataStoragePath]);

            MainApplicationService.Initialize(configuration);
            RegisterControllers();

            // Object-to-object mappings
            RegisterObjectMappings();

            // Custom dependency resolved
            DependencyResolver.SetResolver(new UnityDependencyResolver());


        }

        private void ConfigureBundleTables()
        {
            var env = ConfigurationManager.AppSettings["environment"] ?? "dev";
            BundleTable.Bundles.RegisterTemplateBundles();
            var cb = new Bundle("~/editors");
            
            BundleTable.Bundles.Add(cb);
            //if (!env.Contains("live"))
            //{
            //    var registeredBundles = BundleTable.Bundles.GetRegisteredBundles();
            //    foreach (var bundle in registeredBundles)
            //    {
            //        if (bundle.Transform is System.Web.Optimization.JsMinify)
            //        {
            //            bundle.Transform = new NoTransform();
                        
            //        }
            //    }
                
            //}
        }

        protected void RegisterObjectMappings()
        {
            ObjectMapper.CreateMap<TestInfo, TestModel>(new List<string>() { "ProjectName" });
            ObjectMapper.CreateMap<TestModel, TestInfo>(new List<string>() { "Steps", "Created", "Updated" });
            ObjectMapper.CreateMap<TestConstListCreateModel, TestConstList>(new List<string>() { "ProjectId", "Project", "Id", "Created", "Updated" ,"Constants"});
            ObjectMapper.CreateMap<TestConstList, TestConstListEditModel>(new List<string>() {"ProjectNameOrId"}); 
            ObjectMapper.CreateMap<EasyStep.Attributes.TestAction, EasyStep.Website.Models.TestActionModel>();
            ObjectMapper.Configure();
        }

        protected void RegisterControllers()
        {
            ObjectManager.RegisterType<HomeController>();
            ObjectManager.RegisterType<TestController>();
            ObjectManager.RegisterType<TestProjectController>();
            ObjectManager.RegisterType<TestConstListController>();
            ObjectManager.RegisterType<TestConstsController>();
            ObjectManager.RegisterType<AccountController>();
        }
    }
}